<?php
/**
 * 异常处理类
 * Created by PhpStorm.
 * User: XD
 * Date: 2017/8/25
 * Time: 15:12
 */
namespace app\log;
class Myexception extends \Exception
{
    /**
     * @desc 	异常处理函数
     * @parm 	object $e 异常对象
     */
    public static function exceptionHandler($e){
        self::exception($e,"exception");
    }

    public static function exception($e,$what){
        $file = $e->getFile();
        $line 	= $e->getLine();
        $code 	= $e->getCode();
        $message= $e->getMessage();  

        if(UseLog::$log != null){
            if(class_exists('\app\log\Log',false)){  //class_exists — 检查类是否已定义
                Log::$writeOnAdd = true;
                UseLog::$log->add(3,'错误码'.$code.'=>'.$message,array('file'=>$file,'line'=>$line),$what);
            }
        }
    }

    /**
     * @desc 错误处理函数
     */
    public static function errorHandler($errno,$errstr,$errfile,$errline){
        self::exception(new \ErrorException($errstr,$errno,0,$errfile,$errline),"error");
    }

    public static function shutdownHandler(){
        $error = error_get_last();
        if($error){
            self::exception(new \ErrorException($error['message'],$error['type'],0,$error['file'],$error['line']),"shutdown_err");
        }
    }

}